// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import DataManager from "../manager/DataManager";

const { ccclass, property } = cc._decorator;
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
@ccclass
export default class LoseLayer extends BaseLayer {

    panel: cc.Node = null
    btnClose: cc.Node = null
    btnRestart: cc.Node = null
    btnNext: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('btn_close', this.panel)
        this.btnRestart = cc.find('buttons/btn_restart', this.panel)
        this.btnNext = cc.find('buttons/btn_next', this.panel)
        this.btnClose.on('click', this.onCloseClick, this)
        this.btnRestart.on('click', this.onRestartClick, this)
        this.btnNext.on('click', this.onNextClick, this)
    }

    onDestroy() {
        this.btnClose.off('click', this.onCloseClick, this)
        this.btnRestart.off('click', this.onRestartClick, this)
        this.btnNext.off('click', this.onNextClick, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        SdkManager.instance.toggleBannerAd(true)
        this.btnRestart.getChildByName('ad').active = DataManager.instance.power <= 0
    }

    onDisable() {
        SdkManager.instance.toggleBannerAd(false)
    }

    async onRestartClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (this.btnRestart.getChildByName('ad').active) {
            SdkManager.instance.showVideoAd(async (msg: string) => {
                if (!SdkManager.instance.getPlatform()) {
                    ToastManager.instance.show(msg, { gravity: 'TOP', bg_color: cc.color(102, 202, 28, 255) })
                }
                await StaticInstance.fadeManager.fadeIn()
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.LOSE, false)
                StaticInstance.gameManager.onGameStart()
            }, (msg: string) => {
                ToastManager.instance.show(msg, { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
            })
        } else {
            DataManager.instance.power -= 1
            DataManager.instance.save()
            await StaticInstance.fadeManager.fadeIn()
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.LOSE, false)
            StaticInstance.gameManager.onGameStart()
        }
    }

    onNextClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        SdkManager.instance.showVideoAd(async (msg: string) => {
            if (!SdkManager.instance.getPlatform()) {
                ToastManager.instance.show(msg, { gravity: 'TOP', bg_color: cc.color(102, 202, 28, 255) })
            }
            // 关卡+1
            DataManager.instance.level += 1
            if (DataManager.instance.level > DataManager.instance.levelMax) {
                DataManager.instance.levelMax = DataManager.instance.level
            }
            DataManager.instance.save()
            await StaticInstance.fadeManager.fadeIn()
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.LOSE, false)
            StaticInstance.gameManager.onGameStart()
        }, (msg: string) => {
            ToastManager.instance.show(msg, { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
        })
    }

    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataManager.instance.status = ENUM_GAME_STATUS.UNRUNING
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MAIN, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.LOSE, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }
}
